package com.cjm.algorithm.str;

public class Ti151 {
    public static void main(String[] args) {
        Ti151 ti151 = new Ti151();
        System.err.println(ti151.reverseWords("the sky is blue"));
    }

    public String reverseWords(String s) {
        char[] charArray = s.toCharArray();
        char pre = '@';
        StringBuilder x = new StringBuilder();
        for (int i = 0; i < charArray.length; i++) {
            if ('@' == pre && charArray[i] == ' ') {
                continue;
            }
            if (' ' == pre && charArray[i] == ' ') {
                continue;
            }
            x.append(charArray[i]);
            pre = charArray[i];
        }
        s = x.toString();

        String[] split = s.split(" ");
        int length = split.length;
        for (int i = 0; i < length /2; i++) {
            String item = split[i];
            split[i] = split[length -1-i].trim();
            split[length -1-i] = item.trim();
            System.err.println("OK");
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            sb.append(split[i]).append(" ");
        }
        String string = sb.toString();
        return string.substring(0, string.length()-1);
    }
}
